const fs = require('fs');
const path = require('path');
fs.readdir("./packages", (err, files) => {
  files.forEach((item, index) => {
    fs.readdirSync(path.resolve(__dirname, `./packages/${item}/output`)).forEach((item2) => {
      if (item2.endsWith(".zip")) {
        fs.renameSync(path.resolve(__dirname, `./packages/${item}/output/${item2}`), path.resolve(__dirname, `./packages/${item}/output/插件包.zip`))

      }
      if (item2.endsWith(".png") || item2.endsWith(".jpg")) {
        fs.renameSync(path.resolve(__dirname, `./packages/${item}/output/${item2}`), path.resolve(__dirname, `./packages/${item}/output/preview.png`))

      }
    })
  })
})

fs.readdir("./packages", (err, files) => {
  let pic = [], zipFile = []
  files.forEach((item) => {
    !fs.existsSync(path.resolve(__dirname, `./packages/${item}/output/preview.png`)) && pic.push(item)
    !fs.existsSync(path.resolve(__dirname, `./packages/${item}/output/插件包.zip`)) && zipFile.push(item)
  })
  console.log(`未传入zip包的样例：${JSON.stringify(zipFile, null, 2)}\n未传入预览图的样例：${JSON.stringify(pic, null, 2)}`);
})

const templates = require('./templates.json');
fs.readdir("./packages", (err, files) => {
  let fileMap = files.map((item) => {
    let itemArr = item.split("-")
    let isVue = itemArr[itemArr.length - 2] === "vue" ? true : false
    let picPath = fs.existsSync(path.resolve(__dirname, `./packages/${item}/output/preview.png`))
      ? `https://gitee.com/njsmartdata/sdata-plugins/raw/master/packages/${item}/output/preview.png`
      : ""
    let zipPath = fs.existsSync(path.resolve(__dirname, `./packages/${item}/output/插件包.zip`))
      ? encodeURI(`https://gitee.com/njsmartdata/sdata-plugins/raw/master/packages/${item}/output/插件包.zip`)
      : "暂无"
    let plugin = isVue ? itemArr.slice(0, -2).join("-") : itemArr.slice(0, -1).join("-")
    let pluginName = templates[plugin].name
    let templateType = isVue ? "Vue 2" : "React"
    let sourcePath = `https://gitee.com/njsmartdata/sdata-plugins/tree/master/packages/${item}`
    // return [picPath, pluginName, plugin, "", templateType, sourcePath, zipPath]
    return {
      picPath: picPath ? JSON.stringify([{previewUrl: picPath, url: picPath, name: picPath}]) : "暂无", pluginName, plugin, description: "", templateType, sourcePath, zipPath
    }
  })
  fs.writeFile("./fileMap.json", JSON.stringify(fileMap, null, 2), () => {
  })
  console.log(fileMap);
})
